package com.sofwin.controller;

import com.github.pagehelper.PageInfo;
import com.sofwin.Constants;
import com.sofwin.dto.UserDto;
import com.sofwin.pojo.SysDept;
import com.sofwin.pojo.SysRole;
import com.sofwin.pojo.SysUser;
import com.sofwin.service.DeptService;
import com.sofwin.service.RoleService;
import com.sofwin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
* @Project:
* @Description: 用户管理
* @Author : abiao
* @date 2021/4/17 上午 10:56
*/
@Controller
@RequestMapping("system/users")
public class UserController {

    @Autowired
    private DeptService deptService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private UserService service;


    /**
    * @Author abiao
    * @Description  查询初始化+分页
    * @Date 2021/4/17 上午 10:57
    * @param user 模糊查询时，映射查询条件
    * @param pageNumber 当前页
    * @param pageSize 每页展示记录数
    * @param model 数据视图域
    * @return String 视图：TODO
    */
    @RequestMapping("data")
    public String data(SysUser user, @RequestParam(required = true,defaultValue = "1") Integer pageNumber, @RequestParam(required = true,defaultValue =Constants.DEFAULT_PAGE_SIZE) Integer pageSize, Model model){

        PageInfo<UserDto>  page = service.selectUserByPage(user,pageNumber,pageSize);
        model.addAttribute("data",page.getList());
        model.addAttribute("page",page);
        model.addAttribute("user",user);
        return "user/data";
    }


    /**
    * @Author abiao
    * @Description  用户 单个|批量 删除
    * @Date 2021/4/17 上午 11:04
    * @param ids 需要删除的角色的id
    * @return Map
    */
    @RequestMapping(value = "{ids}",method = RequestMethod.DELETE)
    @ResponseBody
    public Map deletes(@PathVariable Integer[] ids){
        boolean status = service.deleteByIds(ids);
        Map map = new HashMap();
        map.put("status",status);
        return map;
    }

    /**
    * @Author abiao
    * @Description  到 新增|修改 界面
    * @Date 2021/4/17 上午 11:29
    * @param model
    * @param id 被修改用户的id
    * @return String
    */
    @RequestMapping(value = "{id}")
    public String toEdit(Model model, @PathVariable Integer id){
        //查询部门信息
        List<SysDept> depts = deptService.selectAllDepts();
        model.addAttribute("depts",depts);
        //查询角色信息
        List<SysRole> roles = roleService.selectAllRoles();
        model.addAttribute("roles",roles);
        // 判断是否为 修改
        if (id!=null && id!=0){
            SysUser user = service.selectUserById(id);
            model.addAttribute("user",user);
        }
        return "user/edit";
    }


    /**
    * @Author abiao
    * @Description 添加|修改 用户信息
    * @Date 2021/4/16 下午 7:31
    * @param user 用户信息
    * @return Map
    */
    @RequestMapping(method = {RequestMethod.POST,RequestMethod.PUT})
    @ResponseBody
    public Map save(SysUser user){
        boolean status = service.saveUser(user);
        Map map = new HashMap();
        map.put("status",status);
        return map;
    }






}
